<?php require_once('../../../Connections/shopcarda1.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

$colname_supplier_brand_r = "-1";
if (isset($_GET['sb_id'])) {
  $colname_supplier_brand_r = $_GET['sb_id'];
}
mysql_select_db($database_shopcarda1, $shopcarda1);
$query_supplier_brand_r = sprintf("SELECT * FROM supplier_brand WHERE sb_id = %s", GetSQLValueString($colname_supplier_brand_r, "int"));
$supplier_brand_r = mysql_query($query_supplier_brand_r, $shopcarda1) or die(mysql_error());
$row_supplier_brand_r = mysql_fetch_assoc($supplier_brand_r);
$totalRows_supplier_brand_r = mysql_num_rows($supplier_brand_r);

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {

if($_FILES['pic']['tmp_name']!="" && $row_supplier_brand_r['pic']!=""){
   $files_upload=$_FILES['pic']['tmp_name'];
   // 取得上傳圖片
  
   // 複製上傳圖片到指定 images 目錄
   copy($_FILES['pic']['tmp_name'], "images/" .$row_supplier_brand_r['pic']); 
   $_POST['pic']=$row_supplier_brand_r['pic'];
}
//------------------------------------------------
if($_FILES['pic']['tmp_name']!="" && $row_supplier_brand_r['pic']==""){
   $file_name = $_FILES['pic']['name'];
   //分割檔名
   $fn_array=explode(".",$file_name);
   //檔名
   $mainName = $fn_array[0];
   //副檔名	
   $subName = $fn_array[1];

   $files_upload=$_FILES['pic']['tmp_name'];
   $files_name= (int)(date("YmdHi"))+rand(0,99999);

   // 複製上傳圖片到指定 images 目錄
   copy($_FILES['pic']['tmp_name'], "images/" .$files_name ."." .$subName); 
   $_POST['pic']= $files_name ."." .$subName;
	}
	
if($_FILES['pic']['tmp_name']=="" && $row_supplier_brand_r['pic']==""){
   $_POST['pic']=$row_supplier_brand_r['pic'];
}
if($_FILES['pic']['tmp_name']=="" && $row_supplier_brand_r['pic']!=""){
   $_POST['pic']=$row_supplier_brand_r['pic'];
}

  $updateSQL = sprintf("UPDATE supplier_brand SET logo_name=%s, logo_name_en=%s, pic=%s WHERE sb_id=%s",
                       GetSQLValueString($_POST['logo_name'], "text"),
                       GetSQLValueString($_POST['logo_name_en'], "text"),
                       GetSQLValueString($_POST['pic'], "text"),
                       GetSQLValueString($_POST['sb_id'], "int"));

  mysql_select_db($database_shopcarda1, $shopcarda1);
  $Result1 = mysql_query($updateSQL, $shopcarda1) or die(mysql_error());

  $updateGoTo = "update.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>編輯</title>
<script type="text/javascript" src="../../js/jquery-1.4.2.min.js"></script> 
<script src="../../js/jquery.alerts/jquery.ui.draggable.js" type="text/javascript"></script>
<script src="../../js/jquery.alerts/jquery.alerts.js" type="text/javascript"></script>
<style type="text/css" title="currentStyle">
			@import "../../js/css/jquery.alerts.css";
		</style>
</head>

<body>
<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="form1" id="form1">
  <table width="620" align="center" cellpadding="5" cellspacing="0">
                        <tr>
                          <td width="80" class="style16">品牌名稱
                          <input name="sb_id" type="hidden" id="sb_id" value="<?php echo $row_supplier_brand_r['sb_id']; ?>" /></td>
                          <td width="200" class="style16"><label for="name"></label>
                          <input name="logo_name" type="text" value="<?php echo $row_supplier_brand_r['logo_name']; ?>" /></td>
                          <td width="90" class="style16">品牌英文名稱</td>
                          <td width="200" class="style16"><label for="en"></label>
                          <input name="logo_name_en" type="text" value="<?php echo $row_supplier_brand_r['logo_name_en']; ?>" /></td>
                        </tr>
                        <tr>
                          <td class="style16">品牌形象圖</td>
                          <td class="style16">
                            <input name="pic" type="file" />
                            </td>
                          <td align="center" class="style16"><?php if ($row_supplier_brand_r['pic']!="") { // Show if recordset not empty ?>
                              <label>
                                <input type="button" name="button6" id="button6" value="預覽" onclick="openWinPic('<?php echo $row_supplier_brand_r['pic']; ?>')" />
                              </label>
                              2:1
  <?php } // Show if recordset not empty ?></td>
                          <td class="style16">&nbsp;</td>
                        </tr>
                        <tr>
                          <td colspan="4" align="center"><input type="submit" name="submit" id="submit" value="確定" />&nbsp;<input type="button" value="取消" onclick="window.close();"></td>
    </tr>
                      </table>
  <input type="hidden" name="MM_update" value="form1" />
</form>
<script>

function openWinPic(pic){
      var printPage = window.open("","printPage","");
      printPage.document.open();
      printPage.document.write("<HTML><head>");
	  printPage.document.write("</head><BODY>");
	  printPage.document.write("<PRE>");
	  printPage.document.write('<table border="0" align="center">');
	  printPage.document.write('<tr><td height="21" align="center"><input type="button" value="關閉" onclick="window.close();"></td></tr>');
	  printPage.document.write('<tr><td>');
      printPage.document.write('<img src="images/'+pic);
	  printPage.document.write('" />');
	  printPage.document.write('</td></tr></table>');
      printPage.document.write("</PRE>");
      printPage.document.close("</BODY></HTML>");
	  
}

</script>
</body>
</html>
<?php
mysql_free_result($supplier_brand_r);
?>
